@extends('admin.layouts.base')

@section('title','控制面板')

@section('pageHeader','控制面板')

@section('pageDesc','DashBoard')

@section('content')

    <div class="row page-title-row" style="margin:5px;">
        <div class="row col-md-8 text-left">
            <h4>&nbsp;&nbsp;<strong class="text-danger">重置Token请及时告诉管理员，不然无法同步更新模板</strong></h4>
            <div class="col-md-4">
                <a href="/admin/group" class="btn btn-primary btn-md">
                    返回分组列表
                </a>
                <a href="/admin/domain/{{auth()->user()->id}}/resetToken" class="btn btn-success btn-md">
                    重置Token
                </a>
            </div>
            <div class="clo-md-6">
                <h4><b>Token：</b>{{Cache::get('userToken')}}</h4>
            </div>
        </div>
        <div class="col-md-4 text-right">
            <a href="/admin/domain/create?gid={{$groupId}}" class="btn btn-success btn-md">
                <i class="fa fa-plus-circle"></i> 添加域名
            </a>
			<a href="javascript:;" onclick="updateTels()" class="btn btn-default btn-md">
                <i class="fa fa-certificate"></i> 批量更新模板
            </a>
        </div>
		 <div class="col-md-2 text-right">
		 
        </div>
    </div>
    <div class="row page-title-row" style="margin:5px;">
        <div class="col-md-6">
		
        </div>
        <div class="col-md-6 text-right">
        </div>
    </div>

    <div class="row">
        <div class="col-xs-12">
            <div class="box">

                @include('admin.partials.errors')
                @include('admin.partials.success')
                <div class="box-body">
                    <table id="tags-table" class="table table-bordered table-hover">
                        <thead>
                        <tr>
                            <th data-sortable="false" class="hidden-sm "><input id="all" type="checkbox">
                                    全选&nbsp;&nbsp; <a href="#" onclick="batchDel()" class="text-danger">删除</a>
                            </th>
                            <th data-sortable="false" class="hidden-sm">序号</th>
                            <th class="hidden-sm">所属用户</th>
                            <th class="hidden-sm">所属分组</th>
                            <th class="hidden-md">域名</th>
                            <th class="hidden-md">分组目录</th>
                            <th class="hidden-md">跳转类型</th>
                            <th class="hidden-md">跳转目标</th>
                            <th class="hidden-md">渲染模板</th>
                            <th class="hidden-md">更新时间</th>
							<th class="hidden-md">推广状态</th>
                            <th data-sortable="false">操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
    </div>
    </div>
    <div class="modal fade" id="modal-delete" tabIndex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">
                        ×
                    </button>
                    <h4 class="modal-title">提示</h4>
                </div>
                <div class="modal-body">
                    <p class="lead">
                        <i class="fa fa-question-circle fa-lg"></i>
                        确认要删除这个域名吗?
                    </p>
                </div>
                <div class="modal-footer">
                    <form class="deleteForm" method="POST" action="/admin/domain">
                        <input type="hidden" name="_token" value="{{ csrf_token() }}">
                        <input type="hidden" name="_method" value="DELETE">
                        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                        <button type="submit" class="btn btn-danger">
                            <i class="fa fa-times-circle"></i>确认
                        </button>
                    </form>
                </div>

            </div>
        </div>
    </div>

    {{--批量删除--}}
    <div class="modal fade" id="modal-batch-del" tabIndex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">
                        ×
                    </button>
                    <h4 class="modal-title">提示</h4>
                </div>
                <div class="modal-body">
                    <p class="lead">
                        <i class="fa fa-question-circle fa-lg"></i>
                        确认要删除这些域名吗?
                    </p>
                </div>
                <div class="modal-footer">
                    <form class="batchDelForm" method="POST" action="/admin/domain">
                        <input type="hidden" name="_token" value="{{ csrf_token() }}">
                        <input type="hidden" name="_method" value="DELETE">
                        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                        <button type="submit" class="btn btn-danger">
                            <i class="fa fa-times-circle"></i>确认
                        </button>
                    </form>
                </div>

            </div>
        </div>
    </div>
@stop

@section('js')
    <script>
        $(function () {
            var table = $("#tags-table").DataTable({
                language: {
                    "sProcessing": "处理中...",
                    "sLengthMenu": "显示 _MENU_ 项结果",
                    "sZeroRecords": "没有匹配结果",
                    "sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
                    "sInfoEmpty": "显示第 0 至 0 项结果，共 0 项",
                    "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
                    "sInfoPostFix": "",
                    "sSearch": "搜索:",
                    "sUrl": "",
                    "sEmptyTable": "表中数据为空",
                    "sLoadingRecords": "载入中...",
                    "sInfoThousands": ",",
                    "oPaginate": {
                        "sFirst": "首页",
                        "sPrevious": "上页",
                        "sNext": "下页",
                        "sLast": "末页"
                    },
                    "oAria": {
                        "sSortAscending": ": 以升序排列此列",
                        "sSortDescending": ": 以降序排列此列"
                    }
                },
                "iDisplayLength": 100,
                order: [[8, "desc"]],
                serverSide: true,
                ajax: {
                    url: '/admin/domain/index',
                    type: 'POST',
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                    },
                    data: function ( d ) {
                        //添加额外的参数传给服务器
                        d.gid = "{{$groupId}}";
                    }
                },
                "columns": [
                    {"data": "action"},
                    {"data": "id"},
                    {"data": "username"},
                    {"data": "gname"},
                    {"data": "domain"},
                    {"data": "directory"},
                    {"data": "go_to_type_title"},
                    {"data": "target_gname"},
                    {"data": "template",render:function(data,type,row){ return "<a href='/admin/templates/"+row['tpl_id']+"/edit'>"+data+"</a>";}},
                    {"data": "updated_at"},
					{"data": "status",render:function(data,type,row){
							if(data==1){
								return '<a onclick="setStatus('+row['id']+');updateTel(\''+row['id']+'\','+row['tpl_id']+',\'{{Cache::get('userToken')}}\');" id="o_'+row['id']+'" style="margin:3px;" href="javascript:;" class="X-Small btn-xs text-success "> <b>关闭推广</b></a>';
							}else{
								return '<a onclick="setStatus('+row['id']+');updateTel(\''+row['id']+'\','+row['tpl_id']+',\'{{Cache::get('userToken')}}\');" id="o_'+row['id']+'" style="margin:3px;" href="javascript:;" class="X-Small btn-xs text-danger ">  <b>开启推广</b></a>';
							}
					}},
                    {"data": "action"}
                ],
                columnDefs: [
                    {
                        'targets': -1, "render": function (data, type, row)
                        {
//                            var caozuo = '<a style="margin:3px;" href="/admin/domain/' + row['id'] + '/edit" class="X-Small btn-xs text-success "><i class="fa fa-edit"></i> 编辑</a>';
                            var caozuo = '<a style="margin:3px;" href="#" attr="' + row['id'] + '" class="delBtn X-Small btn-xs text-danger "><i class="fa fa-times-circle-o"></i> 删除</a>';
                            caozuo += '<a style="margin:3px;" href="javascript:;" onclick="updateTel(\''+row['id']+'\',\''+row['tpl_id']+'\',\'{{Cache::get('userToken')}}\')" class="X-Small btn-xs text-success "><i class="fa fa-edit"></i> 更新模板</a>';
                            caozuo += '<a style="margin:3px;" target="_blank" href="http://' + row['domain'] + '/' + row['directory'] + '" class="X-Small btn-xs text-success "><i class="fa fa-edit"></i> 浏览</a>';
                            caozuo += '<a style="margin:3px;" href="/admin/domainAttr?did=' + row['id'] + '" class="X-Small btn-xs text-warning "> 属性列表</a>';
							
							
                            return caozuo;
                        }
                    },
                    {
                        'targets': 0, "render": function (data, type, row)
                        {
                            var caozuoCB = '<div class="checkbox hidden-sm"><label><input class="itemCB" name="itemCB[]" type="checkbox" value="' + row['id'] + '"></label></div>';
                            return caozuoCB;
                        }
                    }
                ]
            });

            table.on('preXhr.dt', function () {
                loadShow();
            });

            table.on('draw.dt', function () {
                loadFadeOut();
            });

            table.on('order.dt search.dt', function () {
                table.column(1, {search: 'applied', order: 'applied'}).nodes().each(function (cell, i) {
                    cell.innerHTML = i + 1;
                });
            }).draw();

            $("table").delegate('.delBtn', 'click', function () {
                var id = $(this).attr('attr');
                $('.deleteForm').attr('action', '/admin/domain/' + id);
                $("#modal-delete").modal();
            });

            if ("{{Request::get('callback')}}" !== "") {
                var url = "http://{{Request::get('domain')}}/upgrade.php";
                $.post(url, {
                    did: "{{Request::get('did')}}",
                    directory: "{{Request::get('directory')}}",
                    domain: "{{Request::get('domain')}}",
                    time: "{{Request::get('callback')}}"
                });

            }
        });
		function setStatus(id){
			$.ajax({
				url:"/admin/domain/setStatus",
				async:false,
				dataType:'json',
				type:'post',
				headers: {
                        'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                    },
				data:{"id":id},
				success:function(data){
					if(data.status==200){
						$("#o_"+id).html(data.o);
						$("#o_"+id).attr('class',data.c);
					}else{
						alert(data.msg);
					}
				}
			});
		}
    </script>

@stop